/*
 *  Node.c
 *  final
 *
 *  Created by Javier Quevedo on 1/21/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "Node.h"
#include "Model.h"

Node *initNode(){
	Node *newNode = malloc(sizeof(Node));
	newNode->modelPart = NULL;
	newNode->leftChild=NULL;
	newNode->rightSibling=NULL;
	//newNode->rtlMatrix.translation = malloc(sizeof(GLfloat *3));
	//GLfloat trans[3] = {0.0, 0.0, 0.0};
	setNodeScale(newNode, 1.0, 1.0, 1.0);
	setNodeTranslation(newNode, 0.0, 0.0, 0.0);
	setNodeRotation(newNode, 0.0, 0.0, 0.0, 0.0);
	return newNode;
}

void *releaseNode(Node *node){
	if (node->rightSibling!=NULL)
		releaseNode(node->rightSibling);
	
	if (node->leftChild!=NULL)
		releaseNode(node->leftChild);

	free(node);
	
}

void renderNode (Node *node){
	glPushMatrix();
	glTranslated(node->rtlMatrix.translation[0], 
				 node->rtlMatrix.translation[1], 
				 node->rtlMatrix.translation[2]);
	glRotatef(node->rtlMatrix.rotation[0], 
			  node->rtlMatrix.rotation[1], 
			  node->rtlMatrix.rotation[2], 
			  node->rtlMatrix.rotation[3]);
	

	glScaled(node->rtlMatrix.scale[0],
			 node->rtlMatrix.scale[1],
			 node->rtlMatrix.scale[2]);
	
	if (node->objectShape == OScustom)
	{
		renderPart(node->modelPart);
	}
	else if (node->objectShape == OSsphere) {
		
	}
	
	if (node->leftChild!=NULL)
		renderNode(node->leftChild);
	glPopMatrix();
	if (node->rightSibling!=NULL)
		renderNode(node->rightSibling);
	
}
void transcend(Node *node){
	
}

void setModelPart(Node *node, ModelPart *modelPart){
	node->modelPart = modelPart;
}

void addChild(Node *node, Node *leftNode){
	if (!node->leftChild)
		node->leftChild=leftNode;
	else 
	{
		Node *tempNode = node->leftChild;
		node->leftChild=leftNode;
		leftNode->rightSibling = tempNode; 
	}
}

void setObjectShape(Node *node, ObjectShape something){
	node->objectShape= something;
}

void setNodeTranslation(Node *node, GLfloat x, GLfloat y, GLfloat z){
	node->rtlMatrix.translation[0]=x;
	node->rtlMatrix.translation[1]=y;
	node->rtlMatrix.translation[2]=z;
}

void transLateNode(Node *node, GLfloat x, GLfloat y, GLfloat z){
	node->rtlMatrix.translation[0]=node->rtlMatrix.translation[0]+x;
	node->rtlMatrix.translation[1]=node->rtlMatrix.translation[1]+y;
	node->rtlMatrix.translation[2]=node->rtlMatrix.translation[2]+z;	
}


void setNodeScale(Node *node, GLfloat x, GLfloat y, GLfloat z){
	node->rtlMatrix.scale[0] = x;
	node->rtlMatrix.scale[1] = y;
	node->rtlMatrix.scale[2] = z;

}

void scaleNode(Node *node, GLfloat factor, GLfloat x, GLfloat y, GLfloat z){
	node->rtlMatrix.scale[0]= node->rtlMatrix.scale[0] * x;
	node->rtlMatrix.scale[1]= node->rtlMatrix.scale[1] * y;
	node->rtlMatrix.scale[2]= node->rtlMatrix.scale[2] * z;
	
}

void rotateNode(Node *node, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
	node->rtlMatrix.rotation[0] += angle;
	node->rtlMatrix.rotation[1] = x;
	node->rtlMatrix.rotation[2] = y;
	node->rtlMatrix.rotation[3] = z;	
	
}


void setNodeRotation(Node *node, GLfloat angle, GLfloat x, GLfloat y, GLfloat z){
	node->rtlMatrix.rotation[0] = angle;
	node->rtlMatrix.rotation[1] = x;
	node->rtlMatrix.rotation[2] = y;
	node->rtlMatrix.rotation[3] = z;
}

void setSpecular(Node *node, GLfloat vector[4]){
	int i = 0;
	for (i = 0; i<=4; i++)
	{
		node->material.specular[i] = vector[i];
	}
}

void setShininess(Node *node, GLfloat shininess){
	node->material.shininess = shininess;
}

void setDiffuse(Node *node, GLfloat vector[3]){
	int i = 0;
	for (i = 0; i<=3; i++)
	{
		node->material.diffuse[i] = vector[i];
	}
}

void setAmbient(Node *node, GLfloat vector[3]){
	int i = 0;
	for (i = 0; i<=3; i++)
	{
		node->material.ambient[i] = vector[i];
	}
	
}


